package com.jnu.ilearn;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.jnu.ilearn.bean.DataBank;
import com.jnu.ilearn.bean.PointRecord;
import com.jnu.ilearn.bean.User;
import com.jnu.ilearn.utils.UserUtils;

import java.util.ArrayList;
import java.util.List;

public class PointStore extends AppCompatActivity {
    List<StoreItem> storeItemList=new ArrayList<>();
    TextView PointView;
    Context contextMain;
    int Point;
    String account;
    User user;
    private DataBank dataBank;
    private List<PointRecord> pointRecordList= new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_point_store);

        getSupportActionBar().hide();
        ImageView backView=findViewById(R.id.back);
        backView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        contextMain=getBaseContext();
        initData();//加载账单类型图片
        ItemAdapter billsKindAdapter=new ItemAdapter(storeItemList);
        RecyclerView myRecycleView=findViewById(R.id.StoreItemRecycleView);
        dataBank=new DataBank(PointStore.this);
        dataBank.loadData();
        user=dataBank.getUser();
        pointRecordList=dataBank.getPointRecordList();
        Point=user.getPoint();
        PointView =findViewById(R.id.PointNumber);
        PointView.setText(Point+"");
        account=user.getAccount();
        //设置瀑布布局
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
        myRecycleView.setLayoutManager(layoutManager);
        myRecycleView.setAdapter(billsKindAdapter);//设置适配器
    }


    class  ItemAdapter extends RecyclerView.Adapter{
        private List<StoreItem> storeItemList;
        ItemAdapter(List<StoreItem> list){
            storeItemList=list;
        }

        public class ViewHolder extends RecyclerView.ViewHolder{
           private ImageView itemImg;
           private TextView itemName;
           private TextView itemPrice;

            public ViewHolder(@NonNull View itemView) {
                super(itemView);
               this.itemImg=itemView.findViewById(R.id.itemImg);
               this.itemName=itemView.findViewById(R.id.itemName);
               this.itemPrice=itemView.findViewById(R.id.itemPrice);
            }


            public ImageView getItemImg() {
                return itemImg;
            }

            public void setItemImg(ImageView itemImg) {
                this.itemImg = itemImg;
            }

            public TextView getItemName() {
                return itemName;
            }

            public void setItemName(TextView itemName) {
                this.itemName = itemName;
            }

            public TextView getItemPrice() {
                return itemPrice;
            }

            public void setItemPrice(TextView itemPrice) {
                this.itemPrice = itemPrice;
            }
        }
        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.store_item, parent, false);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    TextView costView=view.findViewById(R.id.itemPrice);
                    String costText=costView.getText().toString();
                    TextView itemName=view.findViewById(R.id.itemName);
                    String itemMessage=itemName.getText().toString();
                    costText=costText.substring(0,costText.indexOf("积"));
                    int cost=Integer.parseInt(costText);
                    if(cost>Point){
                        new AlertDialog.Builder(PointStore.this).setTitle("警告").setMessage("目前积分不足以兑换该奖品").show();
                    }
                    else{
                        AlertDialog.Builder builder=new AlertDialog.Builder(PointStore.this);
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() //确认框，点击确定传回数据给主界面
                        {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //扣取金币并且将数据进行更改
                                UserUtils userUtils=new UserUtils();
                                int new_point=Point-cost;
                                Handler handler=new Handler(Looper.getMainLooper()){
                                    @Override
                                    public void handleMessage(@NonNull Message msg) {
                                        super.handleMessage(msg);
                                        if(userUtils.isUpdate()){
                                            Toast.makeText(PointStore.this,"兑换成功",Toast.LENGTH_SHORT).show();
                                            pointRecordList.add(new PointRecord(itemMessage,-cost));
                                            user.setPoint(new_point);
                                            dataBank.saveData();
                                            PointView.setText(new_point+"");
                                        }
                                    }
                                };
                                userUtils.UpdatePoint(handler,account,new_point);
                            }
                        });

                        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });
                        builder.setMessage("是否确定兑换此奖品");
                        builder.setTitle("提示");
                        builder.show();
                    }
                }
            });
            ViewHolder viewholder=new ViewHolder(view);
            //为图片添加点击响应功能来获取被点击的图片的ID
            return  viewholder;
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            ViewHolder ItemHolder=(ViewHolder) holder;
            WindowManager wm = (WindowManager) contextMain
                    .getSystemService(Context.WINDOW_SERVICE);//获取屏幕
            int height = wm.getDefaultDisplay().getHeight();//获取屏幕高度
            ImageView itemImg = ItemHolder.getItemImg();
            itemImg.setImageResource(storeItemList.get(position).getImgId());
            itemImg.setTag(storeItemList.get(position).getImgId());

            ItemHolder.getItemImg().getLayoutParams().height=216;//将图片高度设置为屏幕的六分之一
            ItemHolder.getItemName().setText(storeItemList.get(position).getItemName());
            ItemHolder.getItemPrice().setText(storeItemList.get(position).getItemPrice()+"积分");

        }


        @Override
        public int getItemCount() {
            return storeItemList.size();
        }
    }
    //将账单图片载入
    void initData(){
        storeItemList.add(new StoreItem(R.drawable.phone,"iphone13",200));
        storeItemList.add(new StoreItem(R.drawable.earphone,"airpods",100));
        storeItemList.add(new StoreItem(R.drawable.mdl,"100元代金券",100));
        storeItemList.add(new StoreItem(R.drawable.kfc,"100元代金券",50));
        storeItemList.add(new StoreItem(R.drawable.book,"斗罗大陆全集",100));


    }
}
class StoreItem{
    private int ImgId;
    private String itemName;
    private int itemPrice;

    StoreItem(int ImgId,String itemName,int itemPrice){
        this.ImgId=ImgId;
        this.itemName=itemName;
        this.setItemPrice(itemPrice);
    }
    public int getImgId() {
        return ImgId;
    }

    public void setImgId(int imgId) {
        ImgId = imgId;
    }

    public String getItemName() {
        return itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }


    public int getItemPrice() {
        return itemPrice;
    }

    public void setItemPrice(int itemPrice) {
        this.itemPrice = itemPrice;
    }
}